package pl.com.like.sequoia.jms.client.producer;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

import pl.com.sequoia.jms.JmsMessageData;

@Component
public class JmsMessageProducer implements IJmsMessageProducer {

	private final static Logger log = LoggerFactory.getLogger(JmsMessageProducer.class);

	private JmsTemplate template;

	public JmsTemplate getTemplate() {
		return template;
	}

	public void setTemplate(JmsTemplate template) {
		this.template = template;
	}

	/** {@inheritDoc} */
	public void triggerFileScanning(final Long upFileId) {
		template.send(new MessageCreator() {

			public Message createMessage(Session session) throws JMSException {
				MapMessage mMessage = session.createMapMessage();
				mMessage.setLong(JmsMessageData.UPLOAD_FILE_ID, upFileId);
				if (log.isInfoEnabled()) {
					log.info("information about uploaded file to scan with id {} has been sent via JMS", upFileId);
				}
				return mMessage;
			}
		});
	}
}
